package com.bnyy.medicalHousekeeper.moudle.health.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.health.bean.BloodPressureWeeklyHistory;
import com.bnyy.medicalHousekeeper.R;

import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/9/5
 **/
public class BloodPressureWeeklyHistoryAdapter extends
        BaseNormalListAdapter<BloodPressureWeeklyHistory, BloodPressureWeeklyHistoryAdapter.ViewHolder> {

    private Consumer<BloodPressureWeeklyHistory.Detail> consumer;

    public BloodPressureWeeklyHistoryAdapter(Context mContext, Consumer<BloodPressureWeeklyHistory.Detail> consumer) {
        super(mContext);
        this.consumer = consumer;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_blood_pressure_weekly_history, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        BloodPressureWeeklyHistory data = getData(position);

        holder.tvDate.setText(data.getMonth());

        data.getList().forEach(new Consumer<BloodPressureWeeklyHistory.Detail>() {
            @Override
            public void accept(BloodPressureWeeklyHistory.Detail detail) {
                inflater.inflate(R.layout.divider_with_margin, holder.llDetail, true);

                TextView tvDetail = new TextView(mContext);
                tvDetail.setText(detail.getContent());
                tvDetail.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                tvDetail.setTextColor(ContextCompat.getColor(mContext, R.color.gray_text));

                Drawable arrow = ContextCompat.getDrawable(mContext, R.mipmap.icon_arrow_right_black);
                arrow.setBounds(0, 0, arrow.getMinimumWidth(), arrow.getMinimumHeight());
                tvDetail.setCompoundDrawables(null, null, arrow, null);

                int padding = SizeUtils.dp2px(12);
                tvDetail.setPadding(padding, padding, padding, padding);
                tvDetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        consumer.accept(detail);
                    }
                });
                holder.llDetail.addView(tvDetail, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }
        });
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        TextView tvDate;
        LinearLayout llDetail;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvDate = itemView.findViewById(R.id.tv_date);
            llDetail = itemView.findViewById(R.id.ll_detail);
        }
    }
}


























